package com.data.zfcg;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class DataBaseHelper0705 {
	private String TableNAME;

	private static Logger log = Logger.getLogger(DataBaseHelper0705.class);
	public DataBaseHelper0705(String name) {
		// super(name);
		TableNAME = name;
	}

	// 驱动类
	private static final String driverClass = "com.mysql.jdbc.Driver";
	// 连接地址
	private static final String url = "jdbc:mysql://127.0.0.1:3306?useUnicode=true&characterEncoding=utf8";
	// 用户名
	private static final String uname = "XXXX";
	// 密码
	private static final String pwd = "XXXX";

	/**
	 * 获取数据库连接
	 *
	 * @return 连接对象
	 */
	protected static Connection getConnection() {
		Connection conn = null;
		String sql = null;
		Statement stmt = null;
		// connect the MySQL database
		try {
			Class.forName(driverClass);
			conn = DriverManager.getConnection(url, uname, pwd);
			if (conn != null) {
				// create database and table that will be needed
				try {
					sql = "CREATE DATABASE IF NOT EXISTS XXX DEFAULT CHARSET utf8 COLLATE utf8_general_ci;";
					stmt = conn.createStatement();
					stmt.executeUpdate(sql);

					sql = "USE XXXX ";
					stmt = conn.createStatement();
					stmt.executeUpdate(sql);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					StringWriter sw = new StringWriter();
					e.printStackTrace(new PrintWriter(sw, true));
					String strs = sw.toString();
					log.error("start error:" + strs);
				}
			}

		} catch (ClassNotFoundException e) {
			log.info("找不到驱动类");
		} catch (SQLException e) {
			log.info("建立连接错误！");
		}
		return conn;
	}

	/**
	 * 关闭数据库连接
	 *
	 * @param conn
	 *            数据库连接
	 * @param rs
	 *            结果集
	 * @param pstmt
	 *            命令对象
	 */
	public static void closeAll(Connection conn, ResultSet rs, Statement pstmt) {
		try {
			if (null != rs && !rs.isClosed()) {
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			log.info("关闭结果集出错！");
		}
		try {
			if (null != pstmt && !pstmt.isClosed()) {
				pstmt.close();
				pstmt = null;
			}
		} catch (SQLException e) {
			log.info("关闭命令对象出错！");
		}
		try {
			if (null != conn && !conn.isClosed()) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			log.info("关闭链接出错");
		}
	}

	public void onCreate(String name) throws Exception {
		Connection conn = getConnection();
		Statement stmt = null;
		String sql = null;
		// create a database connection
		// MySQL的JDBC URL编写方式：jdbc:mysql://主机名称：连接端口/数据库的名称?参数=值
		// 避免中文乱码要指定useUnicode和characterEncoding
		// 执行数据库操作之前要在数据库管理系统上创建一个数据库，名字自己定，
		// 下面语句之前就要先创建Jxsggzy数据库
		if (conn != null) {
			// create database and table that will be needed
			try {
				sql = "create table if not exists " + TableNAME + " (pageno int(8) not null,"
						+ "newsclass int(8) not null,areaname varchar(30),typename varchar(30)," + "newstitle varchar(255)," + "newstime DATE not null,"
						+ "isexpire DATE," + "url varchar(150)," + "newsdetails text,"
						+ "autoID int not null auto_increment primary key," + "purchaser varchar(255),"
						+ "companyname varchar(255)," + "bidcompany varchar(90)," + "classify varchar(30),"
						+ "budget float(20,5)," + "payments float(20,5)," + "specification varchar(5),"
						+ "deadlinedate DATE," + "IFB_NO varchar(50)," + "PACKAGENO varchar(50)," + "tele1 varchar(20),"
						+ "tele2 varchar(20)," + "hitcount int(8),"+ "reserve varchar(255)," + "memo varchar(255),"+ "newsdetailshtml mediumtext"
						+ ") engine=InnoDB DEFAULT CHARSET=utf8"; // ,"+"FULLTEXT
																	// (newstitle,
																	// newsdetails)
				stmt = conn.createStatement();
				// stmt.setQueryTimeout(30);

				int result = stmt.executeUpdate(sql);

				// executeUpdate语句会返回一个受影响的行数，如果返回-1就没有成功
				// statement.executeUpdate("delete from "+TableNAME+";");
				if (result != -1) {
					log.info(TableNAME + " ok!");
				}
			} catch (SQLException e) {
				log.info("onCreate中的MySQL操作错误");
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				conn.close();
			}
		}
	}

	public void addnewslist(String tablename, int pageno, int newsclass, String areaname,String typename,String newstitle, String newstime,
			String isexpire, String newsurl, float budget, String newsdetailstext, String newsdetailshtml)
					throws Exception {
		String purchaser = "", classify = "", specification = "N", IFB_NO = "",companyname="",bidcompany="";
		Map<String, String> telephone = new HashMap<String, String>();
		telephone.put("tele" + 1, "");
		telephone.put("tele" + 2, "");
		PreparedStatement prestat = null;
		Connection conn = null;
		float payments = 0;
		// stmt.setQueryTimeout(30); // set timeout to 30 sec.
		String InsertDB = "insert into " + tablename
				+ " (pageno,newsclass,areaname,typename,newstitle,newstime,isexpire,url,newsdetails,newsdetailshtml,purchaser,companyname,bidcompany,classify,budget,payments,specification,IFB_NO,tele1,tele2) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		try {
			log.info("newsurl:" + newsurl);
			conn = getConnection();
			log.info("conn:" + conn);
			if (conn != null) {
				java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(newstime);
				prestat = conn.prepareStatement(InsertDB);
				prestat.setInt(1, pageno);
				prestat.setInt(2, newsclass);
				prestat.setString(3, areaname);
				prestat.setString(4, typename);
				prestat.setString(5, newstitle.trim());
				prestat.setDate(6, new java.sql.Date(date.getTime()));
				
				java.util.Date date2=getisexpire(newsdetailstext);	
				if(date2!=null){
				prestat.setDate(7,new java.sql.Date(date2.getTime()));
				}
				else{
					prestat.setDate(7,null);
				}
				prestat.setString(8, newsurl.trim());
				newsdetailstext = newsdetailstext.replace("Null", "").trim();
				prestat.setString(9, newsdetailstext.trim());
				prestat.setString(10, newsdetailshtml.trim());
				
				int purstart = 0, pursend = 0;
				if (newstitle.indexOf("关于") > -1) {
					if (newstitle.indexOf("关于") > -1) {
						purstart = newstitle.indexOf("关于") + 2;
					}
					if (newstitle.indexOf("小学") > purstart) {
						pursend = newstitle.indexOf("小学") + 2;
					} else if (newstitle.indexOf("中学") > purstart) {
						pursend = newstitle.indexOf("中学") + 2;
					} else if (newstitle.indexOf("学校") > purstart) {
						pursend = newstitle.indexOf("学校") + 2;
					} else if (newstitle.indexOf("学院") > purstart) {
						pursend = newstitle.indexOf("学院") + 2;
					} else if (newstitle.indexOf("大学") > purstart) {
						pursend = newstitle.indexOf("大学") + 2;
					} else if (newstitle.indexOf("医院") > purstart) {
						pursend = newstitle.indexOf("医院") + 2;
					} else if (newstitle.indexOf("厅") > purstart) {
						pursend = newstitle.indexOf("厅") + 1;
					} else if (newstitle.indexOf("局") > purstart) {
						pursend = newstitle.indexOf("局") + 1;
					} else if (newstitle.indexOf("局") > purstart) {
						pursend = newstitle.indexOf("局") + 1;
					} else if (newstitle.indexOf("院") > purstart) {
						pursend = newstitle.indexOf("院") + 1;
					} else if (newstitle.indexOf("所") > purstart) {
						pursend = newstitle.indexOf("所") + 1;
					} else if (newstitle.indexOf("处") > purstart) {
						pursend = newstitle.indexOf("处") + 1;
					} else if (newstitle.indexOf("委员会") > purstart) {
						pursend = newstitle.indexOf("委员会") + 3;
					}else if (newstitle.indexOf("实验室") > purstart) {
						pursend = newstitle.indexOf("实验室") + 3;
					}
					if (pursend<(purstart+4)&&newstitle.indexOf("、") > purstart) {
						pursend = newstitle.indexOf("、");
					}
					if (pursend > purstart) {
						log.info("purstart："+purstart+"pursend："+pursend);
						if(pursend> (purstart+30)){pursend=purstart+30;}
						purchaser = newstitle.substring(purstart, pursend).trim();
					} else {
						//log.info("purstart："+purstart+"pursend："+pursend);
						purchaser = getpurchaser(newsdetailstext).trim();
					}
				} else {
					log.info("no 关于");
					purchaser = getpurchaser(newsdetailstext).trim();
				}
   				
   				if (purchaser.length()>80){
   					purchaser=purchaser.substring(0, 80);
   				}
				prestat.setString(11, purchaser);
			
			    if (newstitle.indexOf("]") > -1&&newstitle.indexOf("关于")>0) {
					
			    	log.info("]"+newstitle.indexOf("]"));
		        	String newstitle1=newstitle.substring(newstitle.indexOf("]"));
		        	log.info("关于"+newstitle1.indexOf("关于"));
		        	if(newstitle1.indexOf("关于")>0&&newstitle1.indexOf("关于")<30){
		            	bidcompany = newstitle1.substring(1, newstitle1.indexOf("关于")).trim();
		            	log.info("bidcompany:"+bidcompany);
		        	}          
				} 
				if(bidcompany.length()<5&&newsdetailstext.indexOf("关于") > 0&&newsdetailstext.indexOf("关于")< 30) {
		        	String newsdetailstext1=newsdetailstext;
		        	newsdetailstext1=newsdetailstext1.replace("Null", "").trim();
		        	newsdetailstext1=newsdetailstext1.replaceAll("[\\t\\n\\r]", "").trim();
		        	log.info(" 关于"+newsdetailstext1.indexOf("关于"));
		        	if(newsdetailstext1.indexOf("关于")>5&&newsdetailstext1.indexOf("关于")<30){
		            	bidcompany = newsdetailstext1.substring(1, newsdetailstext1.indexOf("关于")).trim();
		            	log.info("bidcompany:"+bidcompany);
		        	}   
					
				}
				if(bidcompany.length()<5&&newsdetailstext.indexOf("受") > 0&&newsdetailstext.indexOf("受")< 30) {
		        	String newsdetailstext1=newsdetailstext;
		        	newsdetailstext1=newsdetailstext1.replace("Null", "").trim();
		        	newsdetailstext1=newsdetailstext1.replaceAll("[\\t\\n\\r]", "").trim();
		        	log.info("受"+newsdetailstext1.indexOf("受"));
		        	if(newsdetailstext1.indexOf("受")>5&&newsdetailstext1.indexOf("受")<30){
		            	bidcompany = newsdetailstext1.substring(1, newsdetailstext1.indexOf("受")).trim();
		            	log.info("受:"+bidcompany);
		        	}   
					
				}

   				if (bidcompany.length()>30){
   					bidcompany=bidcompany.substring(0, 30);
   				}
				prestat.setString(13, bidcompany);
				
				if (newstitle.indexOf("[") > -1 && newstitle.indexOf("]") > 0) {
					classify = newstitle.substring(newstitle.indexOf("[") + 1, newstitle.indexOf("]"));
					classify = classify.trim();
				}
   				if (classify.length()>20){
   					classify=classify.substring(0, 20);
   				}
				prestat.setString(14, classify);

				if (budget == 0) {

					budget = getbudget(newsdetailstext,newsurl);

				}

				if (budget > 2147483647) {
					budget = 0;
				}
				if (budget > 10000) {
					specification = "D";
				}
				if (budget > 300000) {
					specification = "C";
				}
				if (budget > 800000) {
					specification = "B";
				}
				if (budget > 2000000) {
					specification = "A";
				}
				if (budget > 5000000) {
					specification = "S";
				}
				if(budget>10000000){
					specification = "Q";
				}
				if(budget>50000000){
					specification = "W";
				}
				if(budget>100000000){
					specification = "Y";
				}
				if(budget>999999999){
					specification = "G";
				}

				if (newsdetailstext.indexOf("单一来源") == -1) {
					if (newstitle.length() > 10) {
						IFB_NO = getIFB_NO(newstitle);
					}
					if (IFB_NO.length() < 1 && newsdetailstext.length() > 10) {
						IFB_NO = getIFB_NO(newsdetailstext);
					}
				} else {
					IFB_NO = "单一来源";
				}

				log.info("招标？" + newstitle.indexOf("中标") + newstitle.indexOf("结果") + newstitle.indexOf("成交")
						+ newsdetailstext.indexOf("单一来源"));
				if (newsdetailstext.indexOf("中标必须独立完")==-1&&(newstitle.indexOf("中标") > 0 || newstitle.indexOf("结果") > 0 || newstitle.indexOf("成交") > 0
						|| (newsdetailstext.indexOf("单一来源") < 200&&newsdetailstext.indexOf("单一来源")>0)|| (newsdetailstext.indexOf("中标") <  200&&newsdetailstext.indexOf("中标")>0)
						|| (newsdetailstext.indexOf("结果") <  200&&newsdetailstext.indexOf("结果") >0)|| (newsdetailstext.indexOf("成交") <  200&&newsdetailstext.indexOf("成交")>0))) {	
					log.info("中标公告");
					payments = budget;
					budget = 0;
					companyname=getcompany(newsurl,newsdetailstext);		
				} else {
					log.info("招标公告");
					companyname = "";
				}
				if(newsdetailstext.indexOf("废标理由") >0||newstitle.indexOf("废标公告") >0){
					payments=0;
					budget = 0;	
				}
   				if (companyname.length()>50){
   					companyname=companyname.substring(0, 50);
   				}
				prestat.setString(12, companyname.trim());
				log.info("budget:" + budget + "payments:" + payments);
				prestat.setFloat(15, budget);
				prestat.setFloat(16, payments);
				prestat.setString(17, specification);
				prestat.setString(18, IFB_NO);
				telephone = getTelePhone(newsdetailstext);
				// log.info("telephone.get 1)"+telephone.get("tele" +
				// 1));
				prestat.setString(19, telephone.get("tele" + 1));
				prestat.setString(20, telephone.get("tele" + 2));
				//log.info(newstitle + " ok!");
				System.out.println(newstitle + " ok!");
				int result = prestat.executeUpdate();
				// log.info("newstitle:"+newstitle);
				if (result != -1) {
					// log.info(newstitle+" ok!");
				}
			}
		} catch (SQLException e) {
			log.info("addnewslist中的MySQL操作错误");
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("newsurl:"+newsurl+"newstitle:"+newstitle+"comanyname"+companyname+"purchaser"+purchaser+"addnewslist error:" + strs);
		} catch (Exception e) {
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("newsurl:"+newsurl+"newstitle:"+newstitle+"comanyname"+companyname+"purchaser"+purchaser+"addnewslist error:" + strs);
		} finally {
			closeAll(conn, null, prestat);
		}
	}

	
	/**
	 * 抓取招标编号
	 */

	private Date getisexpire(String news_details_text) {
		java.util.Date date = null;
		String isexpire = "",detailtext1="";		
		try {	
			int purstart = 0,kk=5;
				if (news_details_text.length() > 6&&news_details_text.indexOf("开标时间：")>-1) { 
					purstart=news_details_text.indexOf("开标时间：");
					}
				if (news_details_text.length() > 6&&news_details_text.indexOf("开标时间为")>-1) { 
					purstart=news_details_text.indexOf("开标时间为");
					}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("竞标时间：")>-1) { 
					purstart=news_details_text.indexOf("竞标时间：");
					}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("竞谈时间：")>-1) { 
					purstart=news_details_text.indexOf("竞谈时间：");
					}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("开启时间：")>-1) { 
					purstart=news_details_text.indexOf("开启时间：");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("开封时间：")>-1) { 
					purstart=news_details_text.indexOf("开封时间：");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("竞谈时间：")>-1) { 
					purstart=news_details_text.indexOf("竞谈时间：");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("谈判时间：")>-1) { 
					purstart=news_details_text.indexOf("谈判时间：");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("磋商时间：")>-1) { 
					purstart=news_details_text.indexOf("磋商时间：");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("接收时间:")>-1) { 
					purstart=news_details_text.indexOf("接收时间:");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("询价时间：")>-1) { 
					purstart=news_details_text.indexOf("询价时间：");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("报价时间：")>-1) { 
					purstart=news_details_text.indexOf("报价时间：");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("谈判时间为")>-1) { 
					purstart=news_details_text.indexOf("谈判时间为");
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("询价时间为：")>-1) { 
					purstart=news_details_text.indexOf("询价时间为：");
					kk=6;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("询价采购时间：")>-1) { 
					purstart=news_details_text.indexOf("询价采购时间：");
					kk=7;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("开标时间、地点：")>-1) { 
					purstart=news_details_text.indexOf("开标时间、地点：");
					kk=8;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("开启时间、地点：")>-1) { 
					purstart=news_details_text.indexOf("开启时间、地点：");
					kk=8;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("谈判时间、地点：")>-1) { 
					purstart=news_details_text.indexOf("谈判时间、地点：");
					kk=8;
				}	
				else if (news_details_text.length() > 6&&news_details_text.indexOf("谈判时间和地点:")>-1) { 
					purstart=news_details_text.indexOf("谈判时间和地点:");
					kk=8;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("询价时间、地点：")>-1) { 
					purstart=news_details_text.indexOf("询价时间、地点：");
					kk=8;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("开标时间、地点：")>-1) { 
					purstart=news_details_text.indexOf("开标时间、地点：");
					kk=8;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("谈判及报价时间：")>-1) { 
					purstart=news_details_text.indexOf("谈判及报价时间：");
					kk=8;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("磋商时间、地点：")>-1) { 
					purstart=news_details_text.indexOf("磋商时间、地点：");
					kk=8;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("投标截止时间及地点：")>-1) { 
					purstart=news_details_text.indexOf("投标截止时间及地点：");
					kk=10;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("接收标书时间：")>-1) { 
					purstart=news_details_text.indexOf("接收标书时间：");
					kk=7;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("和开标时间定于")>-1) { 
					purstart=news_details_text.indexOf("和开标时间定于");
					kk=7;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("开标时间、地点、方式：")>-1) { 
					purstart=news_details_text.indexOf("开标时间、地点、方式：");
					kk=11;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("填写完整后及相关资料于")>-1) { 
					purstart=news_details_text.indexOf("填写完整后及相关资料于");
					kk=11;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("开标时间及投标截止时间：")>-1) { 
					purstart=news_details_text.indexOf("开标时间及投标截止时间：");
					kk=12;
				}
				else if (news_details_text.length() > 6&&news_details_text.indexOf("投标截止时间和开标时间为")>-1) { 
					purstart=news_details_text.indexOf("投标截止时间和开标时间为");
					kk=12;
				}
				if(purstart>0){
					if(news_details_text.length()>(purstart+kk+30)){
					detailtext1=news_details_text.substring(purstart+kk,purstart+kk+30);
					}
					else{
					detailtext1=news_details_text.substring(purstart+kk,news_details_text.length());
					}
		        	if(detailtext1.indexOf("日")>0){
		            	isexpire = detailtext1.substring(0, detailtext1.indexOf("日")+1).trim();
		            	isexpire=isexpire.replaceAll(" +","");
		            	isexpire =isexpire.replaceAll("\\s*", "");
		            	isexpire=isexpire.replaceAll("\\u00A0","");
		            	if(isexpire.length()>8&&isexpire.indexOf("年")>3){
			            	isexpire=isexpire.substring(isexpire.indexOf("年")-4);
			            if(isDate(isexpire)==true){date = new SimpleDateFormat("yyyy年MM月dd日").parse(isexpire.trim());}
		            	}
		        	}
		        	}
			return date;
		} catch (Exception e) {
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("news_details_text:" + news_details_text + "getIFB_NO error:" + strs);
			return date;
		}
	}
	

public boolean isDate(String str){
    int first = str.indexOf('年');
    int second = str.indexOf('月');
    int third=str.indexOf('日');
   
    if(first == second){
       System.out.println("您输入的日期缺少\"-\"符号");
       return false;
    }else{
       String year = str.substring(0, first);
       String month = str.substring(first+1, second);
       String day = str.substring(second+1, third);
       int maxDays = 31;
       //System.out.println(year+"-"+month+"-"+day);
       if(isNumber(year)== false ||isNumber(month)== false ||isNumber(day)== false){
           System.out.println("您输入的日期包含不可用字符");
           return false;
       }else if(year.length() < 4){
           System.out.println("您输入的年份少于四位");
           return false;
       }
       int y = Integer.parseInt(year);
       int m = Integer.parseInt(month);
       int d = Integer.parseInt(day);
       if(m > 12 || m < 1){
           System.out.println("您输入的月份不在规定范围内");
           return false;
       }else if(m == 4 || m == 6 || m == 9 || m == 11){
           maxDays = 30;
       }else if (m == 2) {
           if (y % 4 > 0)
            maxDays = 28;
            else if (y % 100 == 0 && y % 400 > 0)
            maxDays = 28;
            else
            maxDays = 29;
        }if(d <1 || d > maxDays){
        System.out.println("您输入的日期不在规定范围内");
        return false;
        }
    }
    return true;
}

public boolean isNumber(String str){
    final String number = "1234567890";
    for(int i = 0;i < str.length();i++){
           if(number.indexOf(str.charAt(i)) == -1){
                  return false;
           }
    }
    return true;
}
	
	/**
	 * 抓取金额
	 */

	private float getbudget(String news_details_text,String newsurl) {
		String bud_get = "",temp_bud="",temp_bud2="";
		float budget = 0;
		boolean wan = false;
		try {
			log.info("DATABASE GETBUDGET START");
			if (news_details_text.length() > 5) {
				String text = news_details_text.toString();

				for (int i = 1; i <= text.length() - 5; i++) {
					bud_get = "";
					if (text.charAt(i) == '万' && (text.charAt(i + 1) == '元' || text.charAt(i + 1) ==32|| text.charAt(i + 1) ==160|| text.charAt(i + 1) =='\n'||text.charAt(i + 1)==12288)) {
   						if(i>20){temp_bud=text.substring(i-19, i);
   						if(myJob.iswan(temp_bud)==true){
   						wan = true;}  						
   						}
					}
					// log.info("start:i"+text.charAt(i)+"i+1："+text.charAt(i+1));
					if ((text.charAt(i + 1) == '1' // &&(text.charAt(i)==' '
													// ||text.charAt(i)=='：')&&
							|| text.charAt(i + 1) == '2' || text.charAt(i + 1) == '3' || text.charAt(i + 1) == '4'
							|| text.charAt(i + 1) == '5' || text.charAt(i + 1) == '6' || text.charAt(i + 1) == '7'
							|| text.charAt(i + 1) == '8' || text.charAt(i + 1) == '9' ) //|| text.charAt(i + 1) == '0'
							&& text.charAt(i - 1) != '编') {
						// log.info("start:i"+text.charAt(i)+"i+1："+text.charAt(i+1));
						int k = 2;
						bud_get = bud_get + text.charAt(i + 1);
						while ((i+k)<text.length()&&(text.charAt(i + k) == '1' || text.charAt(i + k) == '2' || text.charAt(i + k) == '3'
								|| text.charAt(i + k) == '4' || text.charAt(i + k) == '5' || text.charAt(i + k) == '6'
								|| text.charAt(i + k) == '7' || text.charAt(i + k) == '8' || text.charAt(i + k) == '9'
								|| text.charAt(i + k) == '.' || text.charAt(i + k) == ','
								|| text.charAt(i + k) == '0')) { 
							bud_get = bud_get + text.charAt(i + k);
							k++;
						}
						// log.info("bud_get"+bud_get);
						int dot = 0;
						boolean isfloat = false,ismoney=false;
						if (bud_get.indexOf(".") > 0) {
							dot = bud_get.indexOf(".");
							if (dot + 1 < bud_get.length()) {
								// log.info("dot+1:"+(dot+1)+"bud_get.length():"+bud_get.length()+":"+bud_get.charAt(dot));
								char c = bud_get.charAt(dot + 1);
								if (("0123456789").indexOf(c + "") != -1) {
									isfloat = true;
									log.info("isfloat" + isfloat);
								}
							}
						}
						bud_get = bud_get.replace(",", "");
						if ((i+k+1)<text.length()&&(text.charAt(i + k) == '万' || text.charAt(i + k) == '元' || text.charAt(i + k + 1) == '万'
								|| text.charAt(i + k + 1) == '元' || text.charAt(i) == '：'
								|| ((text.charAt(i) == 32 || text.charAt(i) == 160)
										&& (text.charAt(i + k) == 32 || text.charAt(i + k) == 160)))) { // (text.charAt(i
																										// +
																										// k)
																										// ==32||text.charAt(i
																										// +
																										// k)
																										// ==160)&&
						}
						if(i>37&&text.length()>i+10){
   							temp_bud2=text.substring(i-36, i+10);
   							//System.out.println("i:"+i+temp_bud2);
   							temp_bud2=temp_bud2.replace(" ", "");
   						if((i+k)<text.length()&&temp_bud2.indexOf("像素")==-1&&temp_bud2.indexOf("账")==-1&&temp_bud2.indexOf("号")==-1&&temp_bud2.indexOf("保证金为")==-1&&temp_bud2.indexOf("注册资金")==-1&&temp_bud2.indexOf("邮")==-1&&temp_bud2.indexOf("电")==-1&&temp_bud2.indexOf("行")==-1&&temp_bud2.indexOf("联系人")==-1&&temp_bud2.indexOf("电话")==-1&&text.charAt(i + k) !='号'){
   						isfloat = true;}  
   						else{isfloat=false;}
   					if(temp_bud2.indexOf("预算")>0||temp_bud2.indexOf("金额")>0||temp_bud2.indexOf("元")>0||temp_bud2.indexOf("万")>0||temp_bud2.indexOf("￥: ")>0){
   			       ismoney = true;}
   						}
						// text.charAt(i + k)==' '为什么验证不了空格,160是不能压缩的空格
						// char c=text.charAt(i + k);
						// int b=c;
						// byte bb=(byte)c;
						// char d=' ';
						// int f=d;
						// byte ff=(byte)d;
						// System.out.println("bud_get:"+bud_get+"b:"+b+"bb:"+bb+"f:"+f+"ff:"+ff+"text.charAt(i
						// + k):"+text.charAt(i + k)+":"+text.charAt(i + k+1));
	   	   				//log.info("temp_bud:"+temp_bud+"isfloat:" + isfloat+"ismoney:"+ismoney);
						//System.out.println("bud_get:" + bud_get + "i+k:"+text.charAt(i + k) + "i+k+1:" + text.charAt(i + k + 1));

						if (text.length()>(i+k)&&isfloat == true && (ismoney==true||(text.charAt(i + k + 1) == '元'  
								|| text.charAt(i + k) == '万' || text.charAt(i + k) == '元'))) {
						//if (isfloat == true && (ismoney==true&&(text.charAt(i + k + 1) == '元' || text.charAt(i + k) == 160
							//	|| text.charAt(i + k) == ' ' || text.charAt(i + k) == '\n' || text.charAt(i + k) == ' '
								//|| text.charAt(i + k) == '万' || text.charAt(i + k) == '元'))) { //
   							if ((text.charAt(i + k+1) == '万'
   									&& (text.charAt(i + k + 2) == '元' ))||(text.charAt(i + k) == '万'
   									&& (text.charAt(i + k + 1) == '元' || text.charAt(i + k + 1) == ' ')
   									|| wan == true)||(text.charAt(i + k) == '万'&&text.charAt(i) == '：'&&text.charAt(i - 1) == '算')) {
   								budget = myJob.Strtofloat(bud_get.trim()) *10000;
                                if((news_details_text.indexOf("县")>0||news_details_text.indexOf("市")>0)&&(news_details_text.indexOf("电脑")>0||
                                		news_details_text.indexOf("摄像机")>0)&&budget>100000000){  //县城电脑等超过1亿
                             	budget=budget/10000;
                                }
   								if(budget>999999900){budget=budget/10000;}  //大于9个亿，网站错误金额
   								// budget
   								// =Float.parseFloat(bud_get.trim())*10000;
   							} else {
   								budget = myJob.Strtofloat(bud_get.trim());
   								
   								if(budget>999999900){budget=budget/10000;
   								log.info("数据太大" + budget);
   								budget=0;}  //大于9个亿，网站错误金额   								   								
   								// budget =Float.parseFloat(bud_get.trim());
   							}
							//log.info("budget" + budget);
							if ((budget != 1.0 && budget != 10000.0) && budget >= 20000.0 && (budget < 999999999)) { // 整型最大值：2147483647,214亿，2万-10亿
								log.info("数据正常" + budget);
								break;
							} else {
								log.info("数据异常," + budget + "，结果清0");
								budget = 0;
							}
						}
					
					}
					
					if(bud_get.length()>1&&(bud_get.length()+i)<text.length() - 5){
						i=bud_get.length()+i-1;  //对不正常数据整个长度直接跳过
						}
				}

			}
			log.info("final bud_get" + bud_get);
			return budget;
		} catch (Exception e) {
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("newsurl:"+newsurl+"news_details_text" + news_details_text + "Databasehelper getbudget error:" + strs);
			return budget;
		}
	}

	static boolean isfloat(String text) {
		char[] b = text.toCharArray();
		boolean result1 = true;
		try {
			if (("123456789.").indexOf(b[0] + "") == -1) {

				result1 = false;
		}
			else{
				for (int i = 1; i < b.length; i++) {
					if (("0123456789.").indexOf(b[i] + "") == -1) {
						result1 = false;
						break;
					}
				}				
			}
			return result1;
		} catch (Exception e) {
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("isfloat error:" + strs);
			return result1;
		}
	}

	/**
	 * 抓取单位名称
	 */

	private String getpurchaser(String news_details_text) {
		String purchaser = "";
		try {
			if (news_details_text.length() > 6) {
				String text = news_details_text.toString();
				for (int i = 0; i <= text.length() - 6; i++) {
					// if ((text.charAt(i) == '单' && text.charAt(i + 1) == '位'&&
					// text.charAt(i+2) == '名'
					// && text.charAt(i + 3) == '称')||(text.charAt(i) == '项' &&
					// text.charAt(i + 1) == '目'&& text.charAt(i+2) == '编'
					// && text.charAt(i + 3) == '号'))
					purchaser = "";
					if ((text.charAt(i) == '单' && text.charAt(i + 1) == '位' && text.charAt(i + 2) == '名'
							&& text.charAt(i + 3) == '称' ) //&& text.charAt(i - 1) == '序' && text.charAt(i - 2) == '排'
							|| (text.charAt(i) == '建' && text.charAt(i + 1) == '设' && text.charAt(i + 2) == '单'
									&& text.charAt(i + 3) == '位'
									&& (text.charAt(i + 4) == 32 || text.charAt(i + 4) == 160)
									&& (text.charAt(i - 1) == 32 || text.charAt(i - 1) == 160))) {
						int k = 5;
						if (text.charAt(i + 4) == 32 || text.charAt(i + 4) == 160) {
							k = 5;
							if (text.charAt(i + 5) == 32 || text.charAt(i + 5) == 160) {
								k = 6;
							}
						}

						while ((i + k) < text.length() && text.charAt(i + k) != 32 && text.charAt(i + k) != 160
								&& text.charAt(i + k) != '\n' && k < 30) {
							purchaser = purchaser + text.charAt(i + k);
							k++;
						}
					}
					if ((text.charAt(i) == '采' && text.charAt(i + 1) == '购' && text.charAt(i + 2) == '单'
							&& text.charAt(i + 3) == '位')) {
						int k = 4;
						// char c=text.charAt(i + 4);
						// char d=text.charAt(i + 5);
						// char e=text.charAt(i + 6);
						// char f=text.charAt(i + 7);
						// int cc=c,dd=d,ee=e,ff=f;

						// log.info("采购单位" +text.charAt(i +
						// 4)+text.charAt(i + 5)+text.charAt(i +
						// 6)+text.charAt(i + 7));
						// log.info("cc"+cc+"dd"+dd+"ee"+ee+"ff"+ff);
						while ((text.charAt(i + k) == 32 || text.charAt(i + k) == 160) && k < 10) {
							k++;
						}
						while (text.charAt(i + k) != 32 && text.charAt(i + k) != 160 && text.charAt(i + k) != '\n'
								&& k < 30) {
							purchaser = purchaser + text.charAt(i + k);
							// int kk=text.charAt(i + k);
							// log.info("k"+text.charAt(i +
							// k)+"kk"+kk);
							k++;
						}
					}

					if ((text.charAt(i) == '招' && text.charAt(i + 1) == '标' && text.charAt(i + 2) == '人'
							&& text.charAt(i + 3) == '名' && text.charAt(i + 4) == '称')) {
						int k = 5;
						if (text.charAt(i + 5) == 32 || text.charAt(i + 5) == 160) {
							k = 6;
							if (text.charAt(i + 6) == 32 || text.charAt(i + 6) == 160) {
								k = 7;
							}
						}

						while (text.charAt(i + k) != 32 && text.charAt(i + k) != 160 && text.charAt(i + k) != '\n'
								&& k < 30) {
							purchaser = purchaser + text.charAt(i + k);
							k++;
						}
					}
					// log.info("purchaser111" +text.charAt(i) +
					// text.charAt(i + 1)+text.charAt(i + 2) );
					if (((text.charAt(i) == '公' && text.charAt(i + 1) == '司')
							|| (text.charAt(i) == '我' && text.charAt(i + 1) == '部')
							|| (text.charAt(i) == '”' && text.charAt(i + 1) == '）')) && text.charAt(i + 2) == '受') {
						int k = 3;
	                       while (text.charAt(i + k) !=32&&text.charAt(i + k) !=160&&text.charAt(i+k) != '\n'&&text.charAt(i + k) != '的'
	                    		   &&text.charAt(i + k) != '、'&&(text.charAt(i + k) != '委'||text.charAt(i + k+1) != '托')
	                    		   &&(text.charAt(i + k) != '（'||text.charAt(i + k+1) != '采')&& k<30) {
							purchaser = purchaser + text.charAt(i + k);
							k++;
						}
					}

					if ((text.charAt(i) == '公' && text.charAt(i + 1) == '司' && text.charAt(i + 2) == '依'
							&& text.charAt(i + 3) == '据')) {
						int k = 4;
						while (text.charAt(i + k) != 32 && text.charAt(i + k) != 160 && text.charAt(i + k) != '\n'
								&& text.charAt(i + k) != '的' && text.charAt(i + k) != '、'
								&& ((text.charAt(i + k) != '委'||text.charAt(i + k+1) != '托')) && k < 30) {
							purchaser = purchaser + text.charAt(i + k);
							k++;
						}
					}

					if ((text.charAt(i) == '中' && text.charAt(i + 1) == '心' && text.charAt(i + 2) == '受')) {
						int k = 3;
						while (text.charAt(i + k) != 32 && text.charAt(i + k) != 160 && text.charAt(i + k) != '\n'
								&& text.charAt(i + k) != '的' && text.charAt(i + k) != '、'
								&& (text.charAt(i + k) != '委'||text.charAt(i + k+1) != '托') && k < 30) {
							purchaser = purchaser + text.charAt(i + k);
							k++;
						}
					}

					if ((text.charAt(i) == '招' && text.charAt(i + 1) == ' ' && text.charAt(i + 2) == '标'
							&& text.charAt(i + 3) == ' ' && text.charAt(i + 4) == '人')) {
						int k = 6;
						while (text.charAt(i + k) != 32 && text.charAt(i + k) != 160 && text.charAt(i + k) != '\n'
								&& text.charAt(i + k) != '；' && k < 30) {
							purchaser = purchaser + text.charAt(i + k);
							k++;
						}
					}

					if ((text.charAt(i) == '采' && text.charAt(i + 1) == '购' && text.charAt(i + 2) == '人'
							&& text.charAt(i + 3) == '：')) {
						int k = 4;
						while (text.charAt(i + k) != 32 && text.charAt(i + k) != 160 && text.charAt(i + k) != '\n'
								&& text.charAt(i + k) != '；' && k < 30) {
							purchaser = purchaser + text.charAt(i + k);
							k++;
						}

					}
					// log.info("text.charAt(i):" + text.charAt(i));
					// log.info("purchaser" + purchaser);
					if (purchaser.length() > 5 && purchaser.indexOf("，") == -1 && purchaser.indexOf("：") == -1
							&& purchaser.indexOf("。") == -1 && purchaser.indexOf("、") == -1) {
						log.info("purchaser" + purchaser);
						break;
					}
				}
			}
				if (purchaser.length()>50){
					purchaser=purchaser.substring(0, 50);
   				}
			return purchaser.trim().replace("：", "");
		} catch (Exception e) {
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("news_details_text" + news_details_text + "getpurchaser error:" + strs);
			return purchaser;
		}
	}

    static String getcompany(String newsurl,String newsdetailstext) { 
       	String companyname="";
       	boolean danyi=false;
       	try{

   			
   			log.info("中标公告"+"companyname:"+companyname);
   			if (newsdetailstext.indexOf("拟由") > 0 && newsdetailstext.indexOf("提供") > 0&&newsdetailstext.indexOf("提供") >newsdetailstext.indexOf("拟由")&&newsdetailstext.indexOf("提供") <newsdetailstext.indexOf("拟由")+50) {
   				log.info("拟由");
   				companyname = newsdetailstext.substring(newsdetailstext.indexOf("拟由") + 2, newsdetailstext.indexOf("提供")).trim();
   				if(companyname.length()<35){danyi=true;}
   			}

   			int start = newsdetailstext.indexOf("第一中标排序单位名称");
   			
   			if (start > 0&&(companyname.length()<6||(companyname.indexOf("公司")==-1&&companyname.indexOf("厂")==-1&&companyname.indexOf("院")==-1&&companyname.indexOf("所")==-1&&companyname.indexOf("学")==-1&&companyname.indexOf("馆")==-1))) {
   				log.info(newsdetailstext.substring(newsdetailstext.indexOf("第一中标排序单位名称")));
   				int end = newsdetailstext.substring(newsdetailstext.indexOf("第一中标排序单位名称")).indexOf("公司");
   				if (end > 0) {
   					String star = newsdetailstext.substring(start);
   					companyname = star.substring(10, end + 2).trim();
   					log.info("start" + start + "end" + (start + end) + "companyname:" + companyname);
   				}
   			}
   			start = newsdetailstext.indexOf("预成交单位：");
   			if (start > 0&&(companyname.length()<6||(companyname.indexOf("公司")==-1&&companyname.indexOf("厂")==-1&&companyname.indexOf("院")==-1&&companyname.indexOf("所")==-1&&companyname.indexOf("学")==-1&&companyname.indexOf("馆")==-1))) {
   				log.info(newsdetailstext.substring(newsdetailstext.indexOf("预成交单位：")));
   				int end = start+6;
   				int k=0;
   				String companyname1="";
   				log.info("预成交单位");
				while (k<30&&(newsdetailstext.charAt(end+k) != 160&&newsdetailstext.charAt(end+k) != 32&&newsdetailstext.charAt(end+k) != '\n')) {
					companyname1 = companyname1 + newsdetailstext.charAt(end + k);
					log.info("k:"+k+"companyname1:"+companyname1);
					k++;
				}
				if(companyname1.length()>5&&((companyname1.indexOf("公司")>0||companyname1.indexOf("院")>0||companyname1.indexOf("所")>0||companyname1.indexOf("学")>0||companyname1.indexOf("馆")>0))){
					companyname=companyname1;
				}
				log.info("companyname1:"+companyname1);
				
   			}
   			
   			start = newsdetailstext.indexOf("中标单位公告如下：");
   			if (start > 0&&(companyname.length()<6||(companyname.indexOf("公司")==-1&&companyname.indexOf("厂")==-1&&companyname.indexOf("院")==-1&&companyname.indexOf("所")==-1&&companyname.indexOf("学")==-1&&companyname.indexOf("馆")==-1))) {
   				log.info(newsdetailstext.substring(newsdetailstext.indexOf("中标单位公告如下：")));
   				int k=9;
   				while(newsdetailstext.charAt(start+k)=='\n'||newsdetailstext.charAt(start+k)==160||newsdetailstext.charAt(start+k)==32){k++;}
   				String companyname1="";
   				log.info(k+"newsdetailstext.charAt(start+k)"+newsdetailstext.charAt(start+k)+"中标单位公告如下：");
				while (k<30&&(newsdetailstext.charAt(start+k) != 160&&newsdetailstext.charAt(start+k) != 32&&newsdetailstext.charAt(start+k) != 12288&&newsdetailstext.charAt(start+k) != '\n'&&newsdetailstext.charAt(start+k) != '，')) {
					companyname1 = companyname1 + newsdetailstext.charAt(start + k);
					log.info("k:"+k+"companyname1:"+companyname1+"newsdetailstext.charAt(start + k):"+newsdetailstext.charAt(start + k));
					k++;
				}
				if(companyname1.length()>5&&((companyname1.indexOf("公司")>0||companyname.indexOf("厂")==-1||companyname1.indexOf("院")>0||companyname1.indexOf("所")>0||companyname1.indexOf("学")>0||companyname1.indexOf("馆")>0))){
					companyname=companyname1;
				}
				log.info("companyname1:"+companyname1);
				
   			}
   			
   			start = newsdetailstext.indexOf("评定，结论是");
   			if (start > 0&&(companyname.length()<6||(companyname.indexOf("公司")==-1&&companyname.indexOf("厂")==-1&&companyname.indexOf("院")==-1&&companyname.indexOf("所")==-1&&companyname.indexOf("学")==-1&&companyname.indexOf("馆")==-1))) {
   				//log.info(newsdetailstext.substring(newsdetailstext.indexOf("中标单位公告如下：")));
   				int k=6;
   				while(newsdetailstext.charAt(start+k)=='\n'||newsdetailstext.charAt(start+k)==160||newsdetailstext.charAt(start+k)==32){k++;}
   				String companyname1="";
   				log.info(k+"newsdetailstext.charAt(start+k)"+newsdetailstext.charAt(start+k)+"结论是");
				while (k<30&&(newsdetailstext.charAt(start+k) != 160&&newsdetailstext.charAt(start+k) != 32&&newsdetailstext.charAt(start+k) != 12288&&newsdetailstext.charAt(start+k) != '\n'&&newsdetailstext.charAt(start+k) != '，')) {
					companyname1 = companyname1 + newsdetailstext.charAt(start + k);
					log.info("k:"+k+"companyname1:"+companyname1+"newsdetailstext.charAt(start + k):"+newsdetailstext.charAt(start + k));
					k++;
				}
				if(companyname1.length()>5&&((companyname1.indexOf("公司")>0||companyname.indexOf("厂")==-1||companyname1.indexOf("院")>0||companyname1.indexOf("所")>0||companyname1.indexOf("学")>0||companyname1.indexOf("馆")>0))){
					companyname=companyname1;
				}
				log.info("companyname1:"+companyname1);
				
   			}
   			
   			
   			if(companyname.length()<6||(danyi==false&&companyname.indexOf("公司")==-1&&companyname.indexOf("中心")==-1&&companyname.indexOf("集团")==-1&&companyname.indexOf("院")==-1&&companyname.indexOf("所")==-1&&companyname.indexOf("学")==-1&&companyname.indexOf("馆")==-1)){
   				companyname=getcompanyname(newsurl);
   			}
   				if (companyname.length()>50){
   					companyname=companyname.substring(0, 50);
   				}
   				return companyname.trim();
   				
       	  } catch(Exception e){
       	  		// TODO Auto-generated catch block
       	  		e.printStackTrace();
       	          StringWriter sw = new StringWriter();   
       	          e.printStackTrace(new PrintWriter(sw, true));   
       	          String strs = sw.toString(); 
       	          log.error("newsurl"+newsurl+"getcompanyname error:"+ strs);
       	          return companyname;
       	          }
       
       }
	
    static String getcompanyname(String newsurl) throws Exception {     	
        String companyname="";
        int jj=0;
        try{
    	org.jsoup.Connection con = Jsoup.connect(newsurl).userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21");
    	con.timeout(18000).ignoreHttpErrors(true).followRedirects(true);
    	Response resp = con.execute();
    	Document doc = null;
    	if (resp.statusCode() == 200) {
        doc = con.get();
        //Element ele = doc.getElementById("TDContent");
        Element infoTable = doc.getElementsByAttributeValue("class", "MsoNormalTable").first();//获得信息表数据
        /*
         * Elements tableLineInfos = infoTable.select("tr"); //对该信息表继续进行筛选，获得一个tr 集合 数据        
        for (Element lineInfo : tableLineInfos) {
            String lineInfoContent = lineInfo.select("td").last().text().trim(); //获得tr集合中的一个td元素
            int result= lineInfoContent.indexOf("预算");
            if(result>0){
            log.info("jsoup is :" + lineInfoContent);
            }
            }
            */        
        if(infoTable !=null){
        	log.info("infoTable抽取开始");
        Elements trs = infoTable.select("tr");
        outer:
        for(int i = 0;i<trs.size();i++){
        	//log.info("trs.size()"+trs.size());
            Elements tds = trs.get(i).select("td");
            //inner:
            for(int j = 0;j<tds.size();j++){
            	//log.info("tds.size()"+tds.size());
            	String text1="";
                String text = tds.get(j).text().trim();         
                //log.info("text :" + text+"i:"+i+"j:"+j+"trs.size()-1):"+(trs.size()-1));
                text = text.replaceAll(" ", "");
                if(text.indexOf("中标单位")>=0||text.indexOf("成交")>=0||text.indexOf("供应商")>=0||text.indexOf("预中标")>=0){
                	//log.info("i:"+i+"j:"+j+"trs.size()"+trs.size()+"trs.get(0).select(td).size()"+trs.get(0).select("td").size()+":trs.get(trs.size()-1).select(td).size()"+trs.get(trs.size()-1).select("td").size());
                /*
                if(trs.get(0).select("td").size()==trs.get(trs.size()-1).select("td").size()&&(trs.size()-1)>0)
                {
             	   jj=(trs.get(1).select("td").size()-1)<j?(trs.get(1).select("td").size()-1):j;
                text1=trs.get(1).select("td").get(jj).text().trim();                
                log.info("trs.get(1).select(td).size()"+trs.get(1).select("td").size()+"company :" + text1+"i:"+1+"jj:"+jj);
                }
                else
                {       */      
                //log.info("budget :" + text1+"i:"+(trs.size()-1)+"j:"+j);
                if(i+1<trs.size()){
             	  // log.info("trs.get(i+1).select(td).size() :" + trs.get(i+1).select("td").size()+"j:"+j);
            	if(trs.get(i+1).select("td").size()<=j)
            	{jj=trs.get(i+1).select("td").size()-1;}
            	else{jj=j;}
            	text1=trs.get(i+1).select("td").get(jj).text().trim();
                }
                //}          
                companyname=text1.trim();
                log.info("companyname is:" +companyname);
                if(companyname.length()>6&&companyname.length()<30&&(companyname.indexOf("经销部")>0||companyname.indexOf("集团")>0||companyname.indexOf("公司")>0||companyname.indexOf("院")>0||companyname.indexOf("所")>0||companyname.indexOf("学")>0))
                {
                break outer;
                }
                }
            }

        }
        
    	}
        log.info("companyname:"+companyname+"companyname.length():"+companyname.length());
        if(companyname.length()<5||companyname.length()>30||(companyname.indexOf("经销部")>0&&companyname.indexOf("集团")>0&&companyname.indexOf("公司")==-1&&companyname.indexOf("院")==-1&&companyname.indexOf("所")==-1&&companyname.indexOf("学")==-1)){
        	Element ele = doc.getElementById("TDContent");
        	log.info("TDContent抽取开始");
            if(ele !=null){
                Elements trs = ele.select("tr");
                outer:
                for(int i = 0;i<trs.size();i++){
                	log.info("trs.size()"+trs.size());
                    Elements tds = trs.get(i).select("td");
                    //inner:
                    for(int j = 0;j<tds.size();j++){
                    	//String text1="";
                        String text = tds.get(j).text().trim();         
                       // log.info("text："+text+"i"+i+"j:"+j+"tds.size()"+tds.size());
                        if(text.length()<30&&(text.indexOf("项目")==-1)&&((text.indexOf("公司")>0||text.indexOf("省")>0)||text.indexOf("市")>0||text.indexOf("县")>0||text.indexOf("院")>0||text.indexOf("所")>0)){
                        	
                        	companyname=text;
                        	//log.info("ok!"+"text："+text+"i"+i+"j:"+j+"companyname"+companyname);
                        		break outer;
                        	}                        	
                        
                    }
                }
             }   	
        }

    	}  
    	companyname=companyname.trim().replace("：", "");
        return companyname.trim().replace("。", "");
    	  } catch(Exception e){
    	  		// TODO Auto-generated catch block
    	  		e.printStackTrace();
    	          StringWriter sw = new StringWriter();   
    	          e.printStackTrace(new PrintWriter(sw, true));   
    	          String strs = sw.toString(); 
    	          log.error("newsurl"+newsurl+"getcompanyname error:"+ strs);
    	          return companyname;
    	          }
}
	
	
	/**
	 * 抓取招标编号
	 */

	private String getIFB_NO(String news_details_text) {

		String IFB_NO = "";
		try {
			if (news_details_text.length() > 6) {
				String text = news_details_text.toString();
				for (int i = 1; i <= text.length() - 6; i++) {
					// log.info("text.charAt(i)" +text.charAt(i));

					if ((text.charAt(i) == '招' && text.charAt(i + 1) == '标' && text.charAt(i + 2) == '编'
							&& text.charAt(i + 3) == '号')
							|| (text.charAt(i) == '项' && text.charAt(i + 1) == '目' && text.charAt(i + 2) == '编'
									&& text.charAt(i + 3) == '号')
							|| (text.charAt(i) == '采' && text.charAt(i + 1) == '购' && text.charAt(i + 2) == '编'
									&& text.charAt(i + 3) == '号')
							|| (text.charAt(i) == '招' && text.charAt(i + 1) == '标' && text.charAt(i + 3) == '编'
									&& text.charAt(i + 4) == '号')) {

						// if ((text.charAt(i) == '招' && text.charAt(i + 1) ==
						// '标'&& text.charAt(i+2) == '编'
						// && text.charAt(i + 3) == '号')){
						int k = 5;
						for (k = 5; (i + k) < text.length() && k < 11
								&& (text.charAt(i + k) == '号' || text.charAt(i + k) == 32 || text.charAt(i + k) == 160
										|| text.charAt(i + k) == '(' || text.charAt(i + k) == '［'); k++) {
						}
						//log.info("k" + k + "(text.charAt(i + k)" + text.charAt(i + k));
						String temp_IFB_NO = "";
						while ((i + k) < text.length() && text.charAt(i + k) != 32 && text.charAt(i + k) != 160
								&& text.charAt(i + k) != '。' && text.charAt(i + k) != ')' && text.charAt(i + k) != ']'
								&& text.charAt(i + k) != '］' && text.charAt(i + k) != '】' && text.charAt(i + k) != '）'
								&& text.charAt(i + k) != ')' && text.charAt(i + k) != '\n' && k < 30) {
							temp_IFB_NO = temp_IFB_NO + text.charAt(i + k);
							k++;
						}
						log.info("temp_IFB_NO" + temp_IFB_NO);
						if (temp_IFB_NO.length() > 5) {
							temp_IFB_NO = temp_IFB_NO.replace(" ", "");
							IFB_NO = temp_IFB_NO;
							// log.info("temp_IFB_NO" + temp_IFB_NO);
							temp_IFB_NO = "";
							break;
						}
					}

				}
			}
			return IFB_NO.trim().replace("：", "");
		} catch (Exception e) {
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("news_details_text:" + news_details_text + "getIFB_NO error:" + strs);
			return IFB_NO;
		}
	}

	/**
	 * 抓取电话号码
	 */
	private Map<String, String> getTelePhone(String news_details_text) {
		HashMap<String, String> telephone = new HashMap<String, String>();
		boolean istele=false;
		try {
			if (news_details_text.length() > 5) {
				int temp_index_telephone = 1;
				String text = news_details_text.toString();
				for (int i = 1; i <= text.length() - 5; i++) {
   					if ((text.charAt(i) == '电')|| (text.charAt(i) == '手')) {
   						
   						/*if ((text.charAt(i) == '电' && text.charAt(i + 1) == '话'
   							|| text.charAt(i) == '电' && text.charAt(i + 2) == '话')
   							|| (text.charAt(i) == '手' && text.charAt(i + 1) == '机'
   									|| text.charAt(i) == '手' && text.charAt(i + 2) == '机')) {
   									
   						int k = 3;
   						if ((text.charAt(i + 2) == '话') || (text.charAt(i + 2) == '机')) {
   							k = 4;
   						}
   									*/
   						int k=3;
   						for(k=3;k<8;k++){
   							if((text.charAt(i + k-2) == '话') || (text.charAt(i + k-2) == '机'))
   							{
   								istele=true;
   								break;
   								
   							}
   							}
   					
						String temp_telephone = "";
						for (; istele==true&&k <= 14 && i + k <= text.length() - 1; k++) { //
							if (text.charAt(i + k) == '1' || text.charAt(i + k) == '2' || text.charAt(i + k) == '3'
									|| text.charAt(i + k) == '4' || text.charAt(i + k) == '5'
									|| text.charAt(i + k) == '6' || text.charAt(i + k) == '7'
									|| text.charAt(i + k) == '8' || text.charAt(i + k) == '9'
									|| text.charAt(i + k) == '-' || text.charAt(i + k) == '0') {
								int kk = 0;
								while ((i+k)<text.length()&&kk < 13 && (text.charAt(i + k) == '1' || text.charAt(i + k) == '2'
										|| text.charAt(i + k) == '3' || text.charAt(i + k) == '4'
										|| text.charAt(i + k) == '5' || text.charAt(i + k) == '6'
										|| text.charAt(i + k) == '7' || text.charAt(i + k) == '8'
										|| text.charAt(i + k) == '9' || text.charAt(i + k) == '-'
										|| text.charAt(i + k) == '0')) {
									temp_telephone = temp_telephone + text.charAt(i + k);
									k++;
									kk++;
									//log.info("temp_telephone" + temp_telephone + "k" + k + "kk" + kk);
								}
								break;
							}
						}

						if (temp_telephone.length() > 5) {
							temp_telephone = temp_telephone.replace("-", "");
							telephone.put("tele" + temp_index_telephone, temp_telephone);
							// log.info("tele" +
							// temp_index_telephone+":"+temp_telephone+"telephone"+telephone);
							temp_index_telephone++;
							temp_telephone = "";
						}
					}
				}
			}
			return telephone;
		} catch (Exception e) {
			e.printStackTrace();
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
			String strs = sw.toString();
			log.error("news_details_text:" + news_details_text + "getTelePhone error:" + strs);
			return telephone;
		}
	}

	/**
	 * 删除指定用户信息
	 * 
	 * @param user
	 *            用户对象
	 * @throws Exception
	 *             抛出异常
	 */
	public static void TBdel(String table_name) throws Exception {
		Connection conn = getConnection();
		Statement stmt = null;
		// stmt.setQueryTimeout(30); // set timeout to 30 sec.
		// String TBdel="delete from "+table_name+";";//TRUNCATE TABLE TABLENAME
		String TBdel = "drop table " + table_name + ";"; // drop table
		if (conn != null) {
			try {
				stmt = conn.createStatement();
				int result = stmt.executeUpdate(TBdel);
				if (result != -1) {
					// log.info(" ok!");
				}
			} catch (SQLException e) {
				log.info("TBdel中的MySQL操作错误");
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeAll(conn, null, stmt);
			}
		}
		conn.close();
	}

	public static void Deduplication(String tablename,String areaname,String typename) throws Exception {
		Connection conn = getConnection();
		Statement stmt = null;
		int DEDBcount = 0;
		// stmt.setQueryTimeout(30); // set timeout to 30 sec.
		String tmp_table_name = "tmp_" + tablename;
		// String CreatTmp="CREATE TABLE if not exists "+tmp_table_name
		// +" (newsclass int(8) not null,"
		// +"newstitle varchar(255),"
		// +"newstime DATE not null ,"
		// +"newsdetails text not null) "
		// +"ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		String CreatTmp = "CREATE TABLE if not exists " + tmp_table_name + " like " + tablename + ";";
		// String InsertTmp="insert into "+tmp_table_name
		// +" select
		// pageno,min(newsclass),newstitle,newstime,isexpire,url,newsdetails "
		// +"from "+table_name+" group by newstitle,newstime,newsdetails"
		// +" having count(*)>1 order by null;";
		// String DelDup="delete a.* from "+table_name
		// +" a,"+tmp_table_name+" b where "
		// + "b.newsdetails=a.newsdetails "
		// + "and b.newstime=a.newstime "
		// +"and b.newstitle=a.newstitle and a.newsclass>b.newsclass;";
		String InsertTmp = "insert into " + tmp_table_name
				+ " select pageno,newsclass,areaname,typename,newstitle,newstime,isexpire,url,newsdetails,max(autoID),purchaser,companyname,"
				+ "bidcompany,classify,budget,payments,specification,deadlinedate,IFB_NO,PACKAGENO,tele1,tele2,hitcount,reserve,memo,newsdetailshtml "
				+ "from " + tablename+" where typename='"+typename+"'" + " group by url" + " having count(*)>1;";
		String DelDup = "delete a.* from " + tablename + " a," + tmp_table_name + " b where "
				+ "b.url=a.url and a.autoID<b.autoID and"+" a.typename='"+typename+"';"; //原来以url比对，但有重复，以title为准。b.url=a.url
		if (conn != null) {
			try {
				stmt = conn.createStatement();
				int DEDBcount1 = DBcount(tablename,typename);
				stmt.executeUpdate(CreatTmp);
				stmt.executeUpdate(InsertTmp);
				int result = stmt.executeUpdate(DelDup);
				int DEDBcount2 = DBcount(tablename,typename);
				DEDBcount = DEDBcount1 - DEDBcount2;
				int DEDBcounttmp = DBcount(tmp_table_name,typename);
				System.out.println("实际重复数据" + DEDBcounttmp + "条,完成" + DEDBcount + "条数据重复删除");
				TBdel(tmp_table_name);
				if (result != -1) {
					// log.info(" ok!");
				}
			} catch (SQLException e) {
				log.info("TBdel中的MySQL操作错误,Deduplication");
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeAll(conn, null, stmt);
			}
		}
		conn.close();
	}

	public void addmaplist(String table_name, List<Map<String, Object>> newslist) throws Exception {
		Connection conn = getConnection();
		Statement stmt = null;
		stmt = conn.createStatement();
		stmt.setQueryTimeout(30); // set timeout to 30 sec.
		String InsertDB = "insert into " + table_name + " values(?,?,?,?,?,?,?);";
		try {
			PreparedStatement stat = conn.prepareStatement(InsertDB);
			/*
			 * for (Map<String, Object> m : newslist) { for (String k :
			 * m.keySet()) { log.info(k + m.get(k));
			 * 
			 * 
			 * } } for (Map<String, Object> m : newslist) { log.info(
			 * "pageno = " + m.get("pageno")); log.info("newsclass = "
			 * + m.get("news_class")); stat.setInt(1, (int) m.get("pageno"));
			 * stat.setInt(2, (int) m.get("news_class"));
			 * stat.setString(3,(String) m.get("news_title"));
			 * stat.setString(4,(String) m.get("newstime"));
			 * stat.setString(5,(String) m.get("isexpire"));
			 * stat.setString(6,(String) m.get("news_url"));
			 * stat.setString(7,(String) m.get("newsdetailstext"));
			 * stat.executeUpdate(); } ResultSet rs=statement.executeQuery(
			 * "select * from "+table_name+";"); while(rs.next()) { // read the
			 * result set log.info("pageno = " +
			 * rs.getString("pageno")); log.info("newsclass = " +
			 * rs.getInt("newsclass")); log.info("newstitle = " +
			 * rs.getInt("newstitle")); } rs.close();
			 */
			stat.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static int DBcount(String tablename,String typename) throws Exception {
		Connection conn = getConnection();
		Statement stmt = null;
		int count = 0;
		// stmt.setQueryTimeout(30); // set timeout to 30 sec.
		String countDB = "select count(*) from " + tablename + " where typename='"+typename+"';";
		try {
			stmt = conn.createStatement();
			// stmt.setQueryTimeout(30); // set timeout to 30 sec.
			ResultSet DBcount = stmt.executeQuery(countDB);
			DBcount.next();
			count = DBcount.getInt(1);
			// statement.executeUpdate("delete from "+TableNAME+";");
			if (count != -1) {
				// log.info(count+" ok!");
			}
			return count;
		} catch (SQLException e) {
			log.info("DBcount中的MySQL操作错误");
			e.printStackTrace();
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			return count;
		} finally {
			conn.close();
		}

	}

	public boolean Comparison(String table_name,String typename, String newstitle, String newstime, String newsurl) throws Exception {
		Connection conn = getConnection();
		Statement stmt = null;
		boolean validate = true;
		String sql = "select url from " + table_name + " where typename='"+typename+"' url='" + newsurl + "';";
		try {
			stmt = conn.createStatement();
			// stmt.setQueryTimeout(30); // set timeout to 30 sec.
			ResultSet rs = stmt.executeQuery(sql);
			log.info("开始比较:" + newstitle + newstime + newsurl);
			if (rs.next()) {// 存在重复
				validate = false;
			}
			log.info(validate);
			return validate;
		} catch (SQLException e) {
			log.info("Comparison中的MySQL操作错误");
			e.printStackTrace();
			return validate;
		} catch (Exception e) {
			e.printStackTrace();
			return validate;
		} finally {
			conn.close();
		}
	}

}
